﻿extern alias ClusterAgg;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;

namespace WindowsFormsApplication1
{
    using ClusterAgg.ClusterAggregation.Datum;
    using ClusterAgg.ClusterAggregation.DataSets;
    using System.Drawing.Drawing2D;

    public partial class CResultGUI : Form
    {
        public Guid id { private set; get; }
        private ClusterAgg.ClusterAggregation.Datum.AData[] m_dataArray;
        private ClusterAgg.ClusterAggregation.DataSets.CPartition m_partition;

        private int m_picWidth;
        private int m_picHeight;
        private string m_filePath;
        private CResultData m_resultData;
        
        public CResultGUI(ClusterAgg.ClusterAggregation.Datum.AData[] arr, ClusterAgg.ClusterAggregation.DataSets.CPartition partition, int w, int h, string path, Guid gid)
        {
            InitializeComponent();
            this.m_partition = partition;
            this.m_dataArray = (ClusterAgg.ClusterAggregation.Datum.AData[])arr;
            this.m_picWidth = w;
            this.m_picHeight = h;
            this.m_filePath = path;
            this.id = gid;            
        }

        
        public CResultGUI(CResultData data)
        {
            InitializeComponent();
            this.m_partition = data.partition;
            this.m_dataArray = (ClusterAgg.ClusterAggregation.Datum.AData[])data.arr;
            this.m_picWidth = data.w;
            this.m_picHeight = data.h;
            this.m_filePath = data.path;
            this.id = data.gid;
            this.Text += " "+data.algorithm;
            this.m_resultData = data;
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void ResultGUI_Load(object sender, EventArgs e)
        {
            Bitmap bitmap = new Bitmap(m_picWidth, m_picHeight);
            for (int i = 0; i < bitmap.Height; i++)
            {
                for (int j = 0; j < bitmap.Width; j++)
                {
                    bitmap.SetPixel(j, i, Color.White);
                }
            }
            this.textBox1.Text = this.m_partition.name;
            this.textBox2.Text = ""+this.m_partition.ANMI;

            Random randomGen = new Random();
            KnownColor[] names = (KnownColor[])Enum.GetValues(typeof(KnownColor));
            List<KnownColor> lnames = new List<KnownColor>();
            lnames.AddRange(names);
            lnames.Remove(Color.White.ToKnownColor());
            lnames.Remove(Color.WhiteSmoke.ToKnownColor());
            lnames.Remove(Color.NavajoWhite.ToKnownColor());
            try
            {
                foreach (var cluster in m_partition.clusters)
                {
                    KnownColor randomColorName = lnames[randomGen.Next(lnames.Count)];
                    Color randomColor = Color.FromKnownColor(randomColorName);
                   
                    lnames.Remove(randomColorName);
                    if (lnames.Count == 0)
                    {
                        lnames.AddRange(names);
                        lnames.Remove(Color.White.ToKnownColor());
                        lnames.Remove(Color.WhiteSmoke.ToKnownColor());
                        lnames.Remove(Color.NavajoWhite.ToKnownColor());
                    }
                    foreach (var pixel in cluster.data)
                    {
                        if (m_dataArray[0] is CPixel)
                        {
                            int x = ((CPixel)pixel).x;
                            int y = ((CPixel)pixel).y;
                            bitmap.SetPixel(x, y, randomColor);
                        }
                        else
                            try
                            {
                                bitmap.SetPixel((int)pixel.coordinates[0], (int)pixel.coordinates[1], randomColor);
                            }
                            catch(Exception )
                            {
                            }
                    }
                }
            }
            catch (Exception)
            {
            }
            pictureBox5.Image = resizeImage(bitmap, pictureBox5.Height, pictureBox5.Width);

            Bitmap grayB = new Bitmap(m_picWidth, m_picHeight);
            if (m_dataArray[0] is CPixel)
            {
                foreach (var pixel in m_dataArray)
                {
                    int x = ((CPixel)pixel).x;
                    int y = ((CPixel)pixel).y;
                    Color c = Color.FromArgb(((int)((CPixel)pixel).greyIntencity), ((int)((CPixel)pixel).greyIntencity), ((int)((CPixel)pixel).greyIntencity));
                    grayB.SetPixel(x, y, c);
                }
            }
            else
            {
                foreach (var pixel in m_dataArray)
                {
                    grayB.SetPixel((int)pixel.coordinates[0], (int)pixel.coordinates[1], Color.Black);
                }
            }
           

            pictureBox2.Image = resizeImage(grayB, pictureBox2.Height, pictureBox2.Width);
            if (m_filePath.EndsWith("dts") || m_filePath.EndsWith("txt"))
            {
                Bitmap bitmapD = new Bitmap(m_picWidth, m_picHeight);
                for (int i = 0; i < bitmapD.Height; i++)
                {
                    for (int j = 0; j < bitmapD.Width; j++)
                    {
                        bitmapD.SetPixel(j, i, Color.White);
                    }
                }
                ClusterAgg.ClusterAggregation.Datum.AData[] dataSet = ClusterAgg.ClusterAggregation.GeneralTools.CDatasetReader.readDataArray(m_filePath);
                foreach (ClusterAgg.ClusterAggregation.Datum.AData data in dataSet)
                {
                    bitmapD.SetPixel((int)data.coordinates[0], (int)data.coordinates[1], Color.Black);
                }
                pictureBox1.Image = resizeImage(bitmapD, pictureBox1.Height, pictureBox1.Width);
            }
            else
            {
                try
                {
                    pictureBox1.Image = resizeImage(new Bitmap(m_filePath), pictureBox1.Height, pictureBox1.Width);
                }
                catch (Exception ex)
                {
                    //stay at default
                    String error_str = ex.Message;
                }
                
            }
            if (this.m_partition.name != null)
                textBox1.Text = this.m_partition.name;
            if (this.m_partition.ANMI != 0)
                textBox2.Text = this.m_partition.ANMI.ToString();
            if (this.m_partition.clusters.Count != 0)
                textBox3.Text = this.m_partition.clusters.Count.ToString();
            if (this.m_resultData != null && this.m_resultData.NumberOfPartitions != 0)
                textBox4.Text = this.m_resultData.NumberOfPartitions.ToString();
        } 

        private Bitmap resizeImage(Bitmap origin, int h, int w)
        {
            Bitmap newImage = new Bitmap(w, h);
            using (Graphics gr = Graphics.FromImage(newImage))
            {
                gr.SmoothingMode = SmoothingMode.AntiAlias;
                gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
                gr.DrawImage(origin, new Rectangle(0, 0, w, h));
            }
            return newImage;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void helpToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void openWithDefault(string name)
        {
            System.Diagnostics.Process myProcess = new System.Diagnostics.Process();

            try
            {
                // true is the default, but it is important not to set it to false
                myProcess.StartInfo.UseShellExecute = true;
                myProcess.StartInfo.FileName = name;
                myProcess.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        private void viewSourceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string path = "http://code.google.com/p/braude-cluster-project";
            openWithDefault(path);
        }

        private void aboutUsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form abt = new About();
            abt.Show();
        }

        private void helpToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            try
            {
                string path = Path.Combine(Environment.CurrentDirectory, @"..\html\index.html");
                openWithDefault(path);
            }
            catch (Exception ) { }
        }

        private void viewSourceToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            string path = "http://code.google.com/p/braude-cluster-project";
            openWithDefault(path);
        }

        private void aboutUsToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            Form abt = new About();
            abt.Show();
        }

        private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult rs = this.saveFileDialog1.ShowDialog();
            if (rs != DialogResult.OK)
                return;

            Stream filestream = this.saveFileDialog1.OpenFile();
            if (filestream != null)
            {

                ClusterAgg.ClusterAggregation.GeneralTools.CDatasetWriter.writeParitionToStream(filestream, this.m_partition);
            }
        }


    }
}
